﻿Imports System.Data.SqlClient
Public Class Frm_Dossier
    Dim marji3 As Integer = 1
    Dim isDateValidate As Boolean = True
    Dim pourDate As String
    Dim faragh As String = ""
    Dim isUnique As Boolean = True
    Public Sub selectYear()
        Dim myDate As Date = Date.Now
        Dim myYear As Integer = myDate.Year
        Comb_annee.SelectedItem = myYear
    End Sub
    Public Sub remplir()
        List_tous.Items.Clear()
        If Comb_selection.Text = "جميع الملفات المتداولــــة" Then
            For Each khib As Dossier In Partager.moi.List_info_fichier
                If khib.Archive = False Then
                    List_tous.Items.Add(New ListViewItem(New String() {khib.id, khib.Num.ToString, khib.Tribunal, khib.Objet, khib.Grade, khib.Methode, khib.Type_appel, khib.Etat_appel, khib.Date_inscr_tribunal}))
                End If
            Next
        ElseIf Comb_selection.Text = "الملفات المحكومة تمهيديا" Then
            Dim kat3i As Boolean = False
            Dim tamhidi As Boolean = False
            For Each khib As Dossier In Partager.moi.List_info_fichier
                kat3i = False
                tamhidi = False
                khib.remplir_List_decisions()
                For Each dic As decisions In khib.List_decisions
                    If dic.Type = "قطعي" Then
                        kat3i = True
                    ElseIf dic.Type = "تمهيدي" Then
                        tamhidi = True
                    End If
                    If kat3i = False And tamhidi = True And khib.Archive = False Then
                        List_tous.Items.Add(New ListViewItem(New String() {khib.id, khib.Num.ToString, khib.Tribunal, khib.Objet, khib.Grade, khib.Methode, khib.Type_appel, khib.Etat_appel, khib.Date_inscr_tribunal}))
                        Exit For
                    End If
                Next
            Next
        ElseIf Comb_selection.Text = "الملفات المحكومة قطعيــا" Then
            For Each khib As Dossier In Partager.moi.List_info_fichier
                khib.remplir_List_decisions()
                For Each dic As decisions In khib.List_decisions
                    If dic.Type = "قطعي" And khib.Archive = False Then
                        List_tous.Items.Add(New ListViewItem(New String() {khib.id, khib.Num.ToString, khib.Tribunal, khib.Objet, khib.Grade, khib.Methode, khib.Type_appel, khib.Etat_appel, khib.Date_inscr_tribunal}))
                        Exit For
                    End If
                Next
            Next
        ElseIf Comb_selection.Text = "الملفات الغير محكومــــــة" Then
            For Each khib As Dossier In Partager.moi.List_info_fichier
                khib.remplir_List_decisions()
                If khib.List_decisions.Count = 0 And khib.Archive = False Then
                    List_tous.Items.Add(New ListViewItem(New String() {khib.id, khib.Num.ToString, khib.Tribunal, khib.Objet, khib.Grade, khib.Methode, khib.Type_appel, khib.Etat_appel, khib.Date_inscr_tribunal}))
                End If
            Next
        End If

    End Sub

    Public Function UniqueDossier(ByVal Num_Dossier As String, ByVal tribunal As String, ByVal type_apel As String, Optional ByVal id As Integer = 0) As Boolean
        Dim bol As Boolean = True
        For Each dos As Dossier In Partager.moi.List_info_fichier
            If id <> 0 Then
                If id = dos.id Then
                    Continue For
                End If
            End If

            If (dos.Num = Num_Dossier And dos.Tribunal = tribunal And dos.Type_appel = type_apel) Then
                bol = False
                Exit For
            End If
        Next
        Return bol
    End Function
    Private Sub Btn_parc_7ala_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_parc_7ala.Click
        Me.Enabled = False
        Frm_halat_da3wa.Show()
    End Sub

    Private Sub Btn_parc_mahkama_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_parc_mahkama.Click
        Me.Enabled = False
        Frm_Mahkama.Show()
    End Sub

    Private Sub Btn_parc_ta9adi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_parc_ta9adi.Click
        Me.Enabled = False
        Frm_darajat_ta9adi.Show()
    End Sub

    Private Sub Btn_parc_ta3n_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_parc_ta3n.Click
        Me.Enabled = False
        Frm_ta3n.Show()
    End Sub

    Private Sub Btn_parc_naw3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_parc_naw3.Click
        Me.Enabled = False
        Frm_naw3_da3wa.Show()
    End Sub

    Private Sub Btn_rech_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_rech.Click
        Me.Enabled = False
        Partager.frmafect = Me
        Partager.ref = 0
        Frm_Recherche.Show()
        Partager.typee = Me.Name
    End Sub

    Private Sub Btn_ajout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajout.Click
        Try
            If (List_tous.SelectedIndices.Count > 0) Then
                MsgBox("عفوا لا يمكنك الإضافة، لإضافة ملف جديد عليك الضغط على الزر جديد ثم إدخال البيانات", MsgBoxStyle.Critical, "خطأ في الإضافة")
            Else
                    If MsgBox("هل تريد فعلا إضافة هذاالملف ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "إضافة ملف") = MsgBoxResult.Yes Then

                        Dim ramz As String
                        If txt_ramz.Text = "" Then

                            ramz = txt_num_milf.Text + "/" + Comb_annee.Text
                        Else
                            ramz = txt_num_milf.Text + "/" + txt_ramz.Text + "/" + Comb_annee.Text
                        End If

                        If txt_num_milf.Text = "" Then
                            ramz = ""
                        Else
                            'isUnique = UniqueDossier(ramz, Comb_mahkama.Text, Comb_naw3.Text)
                        End If

                        If isUnique Then

                            If isDateValidate Then
                                If TextBox1.Text = "  /  /" Then
                                    pourDate = Nothing
                                Else
                                    pourDate = TextBox1.Text
                                    pourDate = Partager.reverseString(pourDate)
                                End If
                            Dim cli As New Dossier(1, ramz, Comb_mahkama.Text, txt_mawdou3.Text, comb_darajat_ta9adi.Text, Comb_toro9_ta3n.Text, Comb_naw3.Text, pourDate, Comb_halat_da3wa.Text, 0, txt_molahadat.Text)
                                Partager.moi.Ajouter_info_fichier(cli)

                            List_tous.Items.Clear()
                            Comb_selection.SelectedIndex = 0
                                For Each khib As Dossier In Partager.moi.List_info_fichier
                                    'faragh = ""
                                    'If Format(khib.Date_inscr_tribunal, "dd/mm/yyyy") <> "01/00/0001" Then
                                    '    faragh = Format(khib.Date_inscr_tribunal, "dd/mm/yyyy")
                                    'End If

                                    If khib.Archive = False Then
                                    List_tous.Items.Add(New ListViewItem(New String() {khib.id, khib.Num.ToString, khib.Tribunal, khib.Objet, khib.Grade, khib.Methode, khib.Type_appel, khib.Etat_appel, khib.Date_inscr_tribunal}))
                                End If
                            Next
                            Partager.vider(Me)

                            selectYear()
                            MsgBox("تم الإضافة الملف بنجاح", MsgBoxStyle.Information, "نجاح")
                        Else
                            MsgBox("إن هذا التاريخ الذي أدخلت خاطئ", MsgBoxStyle.Critical, "خطأ")
                        End If

                    Else
                        MsgBox("عفوا، يوجد ملف بنفس المعلومات", MsgBoxStyle.Critical, "خطأ")
                    End If

                End If

            End If
        Catch sqlEx As SqlException
            If sqlEx.Message.Contains("RefIX") Then
                MsgBox("عفوا، هذا المرجع استعمل من قبل، المرجو إدخال مرجع آخر", MsgBoxStyle.Critical, "خطأ")
            Else
                MsgBox(sqlEx.Message)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Btn_modif_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_modif.Click
        Try
            If List_tous.SelectedIndices.Count > 0 Then


                If MsgBox("هل تريد فعلا تعديل هذاالملف ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "تعديل ملف") = MsgBoxResult.Yes Then

                    Dim ramz As String
                    If txt_ramz.Text = "" Then

                        ramz = txt_num_milf.Text + "/" + Comb_annee.Text
                    Else
                        ramz = txt_num_milf.Text + "/" + txt_ramz.Text + "/" + Comb_annee.Text
                    End If

                    If txt_num_milf.Text = "" Then
                        ramz = ""
                    Else
                        'isUnique = UniqueDossier(ramz, Comb_mahkama.Text, Comb_naw3.Text, avo.id)
                    End If
                    If isUnique Then

                        If isDateValidate Then
                            If TextBox1.Text = "  /  /" Then
                                pourDate = Nothing
                            Else
                                pourDate = TextBox1.Text
                                pourDate = Partager.reverseString(pourDate)
                            End If
                            Partager.close()
                            Dim cli As New Dossier(1, ramz, Comb_mahkama.Text, txt_mawdou3.Text, comb_darajat_ta9adi.Text, Comb_toro9_ta3n.Text, Comb_naw3.Text, pourDate, Comb_halat_da3wa.Text, 0, txt_molahadat.Text)
                            Partager.moi.modifier_info_fichier(cli, avo.id)

                            List_tous.Items.Clear()
                            Comb_selection.SelectedIndex = 0
                            For Each khib As Dossier In Partager.moi.List_info_fichier
                                'faragh = ""
                                'If Format(khib.Date_inscr_tribunal, "dd/mm/yyyy") <> "01/00/0001" Then
                                '    faragh = Format(khib.Date_inscr_tribunal, "dd/mm/yyyy")
                                'End If

                                If khib.Archive = False Then
                                    List_tous.Items.Add(New ListViewItem(New String() {khib.id, khib.Num.ToString, khib.Tribunal, khib.Objet, khib.Grade, khib.Methode, khib.Type_appel, khib.Etat_appel, khib.Date_inscr_tribunal}))
                                End If
                            Next
                            Partager.vider(Me)
                            selectYear()
                            MsgBox("تم تعديل الملف بنجاح", MsgBoxStyle.Information, "نجاح")
                        Else
                            MsgBox("إن هذا التاريخ الذي أدخلت خاطئ", MsgBoxStyle.Critical, "خطأ")
                        End If

                    Else
                        MsgBox("عفوا، يوجد ملف بنفس المعلومات", MsgBoxStyle.Critical, "خطأ")
                    End If

                End If

            Else
                MsgBox("اختر ملف من القائمة", MsgBoxStyle.Information, "خطأ في التعديل")
            End If
        Catch sqlEx As SqlException
            If sqlEx.ErrorCode = -2146232060 Then
                MsgBox("عفوا، هذا المرجع استعمل من قبل، المرجو إدخال مرجع آخر", MsgBoxStyle.Critical, "خطأ")
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Btn_supp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_supp.Click
        Try
            If List_tous.SelectedIndices.Count > 0 Then
                If MsgBox("هل تريد فعلا حذف هذا الملف ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "حذف ملف") = MsgBoxResult.Yes Then
                    Partager.close()
                    Dim clt As Dossier = Partager.moi.List_info_fichier(List_tous.SelectedIndices.Item(0))
                    Partager.moi.suprimmer_info_fichier(avo.Id)
                    List_tous.Items.Clear()
                    Comb_selection.SelectedIndex = 0
                    For Each khib As Dossier In Partager.moi.List_info_fichier
                        'faragh = ""
                        'If Format(khib.Date_inscr_tribunal, "dd/mm/yyyy") <> "01/00/0001" Then
                        '    faragh = Format(khib.Date_inscr_tribunal, "dd/mm/yyyy")
                        'End If
                        If khib.Archive = False Then
                            List_tous.Items.Add(New ListViewItem(New String() {khib.id, khib.Num.ToString, khib.Tribunal, khib.Objet, khib.Grade, khib.Methode, khib.Type_appel, khib.Etat_appel, khib.Date_inscr_tribunal}))
                        End If
                    Next

                    Partager.vider(Me)
                    MsgBox("عملية الحذف تمت بنجاح", MsgBoxStyle.Information, "نجاح")
                    selectYear()
                End If
            Else
                MsgBox("اختر ملف من القائمة", MsgBoxStyle.Information, "خطأ")
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Public avo As Dossier

    Private Sub List_tous_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles List_tous.DoubleClick
        If Comb_selection.Text = "الملفات المحكومة تمهيديا" Or Comb_selection.Text = "الملفات المحكومة قطعيــا" Or Comb_selection.Text = "الملفات الغير محكومــــــة" Then
            Partager.idDossier = 0
            If List_tous.SelectedIndices.Count > 0 Then
                Partager.idDossier = CInt(List_tous.Items(List_tous.SelectedIndices(0)).SubItems(0).Text)
                Partager.StandardInitial()
                Partager.verifPriv(Frm_a7kam)
            End If
        End If
    End Sub
    Private Sub List_tous_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles List_tous.SelectedIndexChanged
        If List_tous.SelectedItems.Count > 0 Then
            Try
                Partager.close()
                Dim i As Integer = 0
                Dim ref As Integer = CType(List_tous.SelectedItems(0).Text, Integer)
                Dim j As Integer = 0
                While i < List_tous.SelectedIndices.Count
                    avo = Partager.moi.List_info_fichier(j)
                    If avo.id = ref Then
                        If avo.Num.Split("/").Length = 2 Then

                            txt_num_milf.Text = avo.Num.Split("/")(0)
                            txt_ramz.Text = ""
                            Comb_annee.Text = avo.Num.Split("/")(1)

                        ElseIf avo.Num.Split("/").Length = 3 Then

                            txt_num_milf.Text = avo.Num.Split("/")(0)
                            txt_ramz.Text = avo.Num.Split("/")(1)
                            Comb_annee.Text = avo.Num.Split("/")(2)
                        End If


                        Comb_mahkama.Text = avo.Tribunal
                        txt_mawdou3.Text = avo.Objet
                        comb_darajat_ta9adi.Text = avo.Grade
                        Comb_toro9_ta3n.Text = avo.Methode
                        Comb_naw3.Text = avo.Type_appel
                        TextBox1.Text = avo.Date_inscr_tribunal
                        Comb_halat_da3wa.Text = avo.Etat_appel
                        txt_molahadat.Text = avo.Remarque
                        i += 1
                    End If
                    j += 1
                End While
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try

        Else
            Partager.nonInitial.Clear()
            Partager.nonInitial.Add("Comb_selection")
            Partager.vider(Me, Partager.nonInitial)
            selectYear()
        End If
    End Sub

    Private Sub Frm_Dossier_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.ToolTip1.IsBalloon = True

        For i As Integer = 1990 To 2020
            Comb_annee.Items.Add(i)
        Next
        selectYear()
        Try
            Partager.com_ville(Comb_halat_da3wa, "Comb_halat_da3wa", "halat_da3wa")
            Partager.com_ville(Comb_mahkama, "Comb_mahkama", "mahkama")
            Partager.com_ville(comb_darajat_ta9adi, "comb_darajat_ta9adi", "darajat_ta9adi")
            Partager.com_ville(Comb_naw3, "Comb_naw3", "naw3")
            Partager.com_ville(Comb_toro9_ta3n, "Comb_toro9_ta3n", "toro9_ta3n")
            '--------------------------------
            Partager.nonInitial = New ArrayList
            Comb_selection.SelectedIndex = 0
            '--------------------------------

            'Partager.nonInitial.Add("Comb_selection")
            'Partager.nonInitial.Add("List_tous")
            '----------------------------------
            Partager.moi.remplir_List_info_fichier()
            remplir()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Btn_archive_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_archive.Click
        Try
            If List_tous.SelectedIndices.Count > 0 Then
                If MsgBox("هل تريد فعلا إضافة هذا الملف إلى الأرشيف ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "أرشفة ملف") = MsgBoxResult.Yes Then

                    Partager.close()
                    avo.Archive = True
                    Partager.moi.modifier_archive(avo, "1")
                    List_tous.Items.Clear()
                    Comb_selection.SelectedIndex = 0
                    For Each khib As Dossier In Partager.moi.List_info_fichier
                        If khib.Archive = False Then
                            List_tous.Items.Add(New ListViewItem(New String() {khib.id, khib.Num.ToString, khib.Tribunal, khib.Objet, khib.Grade, khib.Methode, khib.Type_appel, khib.Etat_appel, khib.Date_inscr_tribunal}))
                        End If
                    Next
                    MsgBox("تمت أرشفة الملف بنجاح", MsgBoxStyle.Information, "نجاح")
                    Partager.vider(Me)
                    selectYear()
            End If
        Else
        MsgBox("اختر ملف من القائمة", MsgBoxStyle.Information, "خطأ")
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub txt_num_milf_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_num_milf.KeyPress
        Dim allowedChars As String = "1234567890"
        If e.KeyChar = ChrW(8) Then
            e.Handled = False
        ElseIf allowedChars.IndexOf(e.KeyChar) = -1 Then
            ' Invalid Character
            e.Handled = True
        End If
    End Sub

    Private Sub txt_ramz_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_ramz.KeyPress
        Dim allowedChars As String = "1234567890"
        If e.KeyChar = ChrW(8) Then
            e.Handled = False
        ElseIf allowedChars.IndexOf(e.KeyChar) = -1 Then
            ' Invalid Character
            e.Handled = True
        End If
    End Sub

    Private Sub Btn_nouv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_nouv.Click
        Partager.vider(Me)
        selectYear()
    End Sub

    Private Sub txt_marji3ona_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        Dim allowedChars As String = "1234567890"
        If e.KeyChar = ChrW(8) Then
            e.Handled = False
        ElseIf allowedChars.IndexOf(e.KeyChar) = -1 Then
            ' Invalid Character
            e.Handled = True
        End If
    End Sub

    Private Sub TextBox1_TypeValidationCompleted(ByVal sender As Object, ByVal e As TypeValidationEventArgs) Handles TextBox1.TypeValidationCompleted
        If (Not e.IsValidInput) Then
            If (Not e.IsValidInput) Then
                If TextBox1.Text <> "  /  /" Then
                    Me.ToolTip1.ToolTipTitle = "تارخ خاطئ !"
                    Me.ToolTip1.Show("أدخل التاريخ على هذا الشكل: السنة/الشهر/اليوم", Me.TextBox1, 0, -50, 5000)
                    isDateValidate = False
                End If
            End If
            If TextBox1.Text = "  /  /" Then
                isDateValidate = True
            End If
        Else
            isDateValidate = True
        End If
    End Sub

    Private Sub TextBox1_MaskInputRejected(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MaskInputRejectedEventArgs) Handles TextBox1.MaskInputRejected

    End Sub

    Private Sub Comb_selection_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Comb_selection.SelectedIndexChanged
        Partager.nonInitial.Clear()
        Partager.nonInitial.Add("Comb_selection")
        Partager.nonInitial.Add("List_tous")
        Partager.vider(Me, Partager.nonInitial)
        selectYear()
        remplir()
    End Sub
End Class